package com.cch.service.impl;

import com.cch.service.UserDetailService;
import jakarta.annotation.Resource;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cch
 * @date 2025年05月21日 9:37
 */
@Service
public class UserDetailServiceImpl implements UserDetailService {


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("执行自定义登陆逻辑");
        //1.根据用户名去数据库查询，如果不存在抛出UserNameNotFound异常（正常情况下是去数据库查）
        //这里只是一个简单demo
        if(!"cch".equals(username)){
            throw new UsernameNotFoundException("用户名不存在");
        }
        //2.比较密码（注册时已经加密过的，如果匹配成功返回UserDetails）
        return new User(username,"{bcrypt}$2a$10$JhH.vkxRyihUdX9khdWUqOZ/OU6enYXSjb4.BSB2RpJNJB6T8SWRC", AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal,ROLE_abc"));//权限列表
    }

    @Override
    public UserDetails loadUserByPhoneNum(String phoneNum) throws UsernameNotFoundException {
        System.out.println("执行自定义登陆逻辑---手机号");
        //1.根据用户名去数据库查询，如果不存在抛出UserNameNotFound异常（正常情况下是去数据库查）
        //这里只是一个简单demo
        if(!"18888888888".equals(phoneNum)){
            throw new UsernameNotFoundException("用户名不存在");
        }
        List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER", "ROLE_ADMIN");
        return new User(phoneNum,"{bcrypt}$2a$10$JhH.vkxRyihUdX9khdWUqOZ/OU6enYXSjb4.BSB2RpJNJB6T8SWRC", new ArrayList<>());//权限列表
    }
}
